package com.ibm.simpleserver;

import java.net.ServerSocket;
import java.net.Socket;


public class SimpleServer {

	private static final short port = 777;

	public static void main(String args[]) {
		try {
			// creates the ServerSocket and "tells" OS of its
			// interest in requests comming to the specified port
			ServerSocket serverSocket = new ServerSocket(port);

			// create the Queue that holds the requests
			Queue que = new Queue(5, 10);

			// create the Thread pool that contains 5 Threads
			for (int i = 0; i < 5; i++) {
				Thread thread = new TPThread(que);
				thread.start();
			}

			System.out.println("Server started properly");

			// now let's wait and accept connections
			while (true) {
				try {
					// get the request
					Socket request = serverSocket.accept();

					// create the runnable
					Runnable runner = new BasicRunnable(request);

					// add it to the que so a thread can handle it
					que.add(runner);
				}

				catch (java.io.IOException ioe) {
					System.out.println("Problems handling request: " + ioe);
				}
			}
		} catch (Exception e) {
			System.out.println("Problem starting the server....");
			e.printStackTrace(System.out);
			System.exit(-1);
		}
	}
}
